//释放技能
function releaseSkill(hero, boss, skillId) {
  var config = skillConfig[skillId]
  var skill = hero.basicInfo.skills[skillId]
  var skillLvl = skill.lvl
  var skillId = parseInt(skillId)
  if (config.skillType !== 1 || skillLvl <= 0 || skill['timer']) { //不是主动技能
    return
  }

  var cd = config.cd
  var skillFunc = null
  switch (skillId) {
    case 440011:
    case 440015:
    case 440016:
      skillFunc = function() {
        attack1(hero)
        skillAttack1(hero)
        var damage = config.values[skillLvl]
        attack4(hero, boss, damage)
        skillAttack3(hero, boss)
        return damage
      }
      break;
    default:
  }

  if (skillFunc) {
    skillFunc()
    var timerFunc = function() {
      skill['timer'] = setTimeout(function() {
        if (boss.fightAttri.hp > 0) {
          skillFunc()
          timerFunc()
        }
      }, cd)
    }
    timerFunc()
  }
}
//释放大招
function releaseThunder(hero, boss) {
  if (hero.basicInfo.thunderSkill.lvl <= 0) {
    return
  }
  hero.basicInfo.anger = 0
  var skillId = parseInt(hero.basicInfo.thunderSkillId)
  var config = skillConfig[skillId]
  var skillLvl = hero.basicInfo.thunderSkill.lvl
  var buffId = config.buffId
  switch (skillId) {
    case 440014: //魂之挽歌
    case 440018: //射手天赋
      //伤害
      attack1(hero)
      skillAttack1(hero)
      var config = skillConfig[skillId]
      var damage = config.values[skillLvl - 1]
      attack4(hero, boss, damage)
      if (buffId) {
        //触发buff
        triggerBuff(hero, boss, buffId)
      }
      break;
    default:
  }
}

//技能攻击前
function skillAttack1(hero) {
  for (var i in hero.basicInfo.skills) {
    var skill = hero.basicInfo.skills[i]
    if (skill.event) {
      skill.event.emit('skillAttack1')
    }
  }
}

//技能后统计伤害后skillAttack3
function skillAttack3(hero, boss) {
  if (hero.basicInfo.anger < 10) {
    hero.basicInfo.anger += 1
    if (hero.basicInfo.anger >= 10) {
      releaseThunder(hero, boss)
    }
  }
}

//攻击前
function attack1(hero) {
  for (var i in hero.basicInfo.skills) {
    var skill = hero.basicInfo.skills[i]
    if (skill.event) {
      skill.event.emit('attack1')
    }
  }
}

//攻击命中统计伤害前
function attack2(hero) {
  for (var i in hero.basicInfo.skills) {
    var skill = hero.basicInfo.skills[i]
    if (skill.event) {
      skill.event.emit('attack2')
    }
  }
}

//攻击后统计伤害前
function attack3(hero) {
  for (var i in hero.basicInfo.skills) {
    var skill = hero.basicInfo.skills[i]
    if (skill.event) {
      skill.event.emit('attack3')
    }
  }
}

//攻击后统计伤害后
function attack4(hero, boss, damage) {
  boss.fightAttri.hp = boss.fightAttri.hp - damage
  if (boss.fightAttri.hp <= 0) {
    damage -= boss.fightAttri.hp
    boss.fightAttri.hp = 0
    var lvl = boss.basicInfo.lvl + 1
    if (bossConfig[lvl]) {
      boss.fightAttri = new BossFightAttri(bossConfig[lvl])
      boss.basicInfo = new BossBasicInfo(bossConfig[lvl])
    }
  }

  setBossHp(boss)
  getGoldBydamage(hero, damage)
  showBattleMsg(damage)
  for (var i in hero.basicInfo.skills) {
    var skill = hero.basicInfo.skills[i]
    if (skill.event) {
      skill.event.emit('attack4')
    }
  }
}

//设置boss的信息
function setBossHp(boss) {
  $('.progress-bar').css('width', Math.ceil(boss.fightAttri.hp / boss.fightAttri.maxHp * 100) + '%')
  $('.hp').html(boss.fightAttri.hp)
  $('.maxHp').html(boss.fightAttri.maxHp)
}


//根据伤害, 获得金币
function getGoldBydamage(hero, damage) {
  hero.basicInfo.damageGold += damage
  if (hero.basicInfo.damageGold > 5) {
    hero.basicInfo.gold += Math.floor(hero.basicInfo.damageGold / 5) * 1
    hero.basicInfo.damageGold = hero.basicInfo.damageGold % 5
  }
}

var msgLen = 0

function showBattleMsg(msg) {
  $('.damageMsg').prepend('<div class="msg">' + msg + '</div>')
  msgLen += 1
}


//技能事件 攻击前attack1 攻击命中attack2 攻击后统计伤害前attack3  攻击后统计伤害后attack4 技能攻击前skillAttack1  技能后统计伤害前skillAttack2
//技能后统计伤害后skillAttack3
function skillEvent(hero, boss, skill, skillId) {
  if (skill.event) {
    return
  }
  skillId = parseInt(skillId)
  switch (skillId) {
    case 440001: //贪婪
      skill.event = Object.create(Emitter.prototype)
      skill.event.on('attack1', function() {
        if (!hero.basicInfo.skills[440001]) {
          return
        }
        var lvl = hero.basicInfo.skills[440001].lvl
        if (lvl <= 0) {
          return
        }
        var value = skillConfig[440001].values[lvl - 1]
        hero.basicInfo.gold += value
        hero.basicInfo.total += value
      })
      break;
    case 440012: //支配死灵
      skill.event = Object.create(Emitter.prototype)
      skill.event.on('skillAttack1', function() {
        if (!hero.basicInfo.skills[440012]) {
          return
        }
        var lvl = hero.basicInfo.skills[440012].lvl
        if (lvl <= 0) {
          return
        }
        //支配死灵buff
        var buff = hero.basicInfo.buffs[450012]
        if (buff) {
          addBuff(hero, boss, buffId)
        }
      })
      break
    default:
  }
}

//触发buff
function triggerBuff(hero, boss, buffId) {
  var cd = buffConfig[buffId].buffTime
  if (cd === 0 && hero.basicInfo.buffs[buffId]) {
    var buff = hero.basicInfo.buffs[buffId]
    if (buff.stack < buff.maxStack) {
      buff.stack += 1
    } else {
      return
    }
  }
  hero.calcHeroAttri(boss)
}

//添加buff
function addBuff(hero, boss, buffId) {
  var cd = buffConfig[buffId].buffTime
  if (cd >= 0) {
    var skillId = buffConfig[buffId].skillId
    var skillCon = skillConfig[skillId]
    var maxStack = 1
    if (skillCon.maxStackBuff) {
      maxStack = skillCon.maxStackBuff[hero.basicInfo[skillId].lvl - 1]
    }
    var stack = 1
    if (hero.basicInfo.buffs[buffId]) {
      clearTimeout(hero.basicInfo.buffs[buffId].timer)
      if (hero.basicInfo.buffs[buffId].stack < maxStack) {
        stack = hero.basicInfo.buffs[buffId].stack + 1
      }
    }
    hero.basicInfo.buffs[buffId] = {
      cd: buffConfig[buffId].buffTime, //buffcd 0永久
      maxStack: maxStack,
      stack: stack,
      timer: null, //定时器
    }
    hero.basicInfo.buffs[buffId].timer = setTimeout(function() {
      delete hero.basicInfo.buffs[buffId]
      hero.calcHeroAttri(boss)
    }, buffConfig[buffId].buffTime)
  }

  hero.calcHeroAttri(boss)
}

//计算普通伤害
function calcDamage(attacker, defender) {
  //计算闪避
  if (Math.random() * 100 < defender.dodge) {
    return 0
  }

  var defend = defender.defend - attacker.puncture
  var damage = 0
  if (defend >= 0) {
    damage = attacker.attack * attacker.attack / (defend + attacker.attack) //计算伤害
  } else {
    damage = attacker.attack * (1 - defend / 50) //计算伤害
  }

  //计算暴击
  if (Math.random() * 100 < attacker.critRate) {
    damage *= (1 + attacker.critHurt / 100)
    critFlag = 1
  }
  if (damage <= 1) {
    damage = 1
  }
  return damage
}
